/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.krazevina.kar;

import java.util.Vector;
import javax.microedition.rms.RecordStore;

/**
 *
 * @author XXX
 */
public class Song {
    public int ID;
    public String name,rawname,lyric,rawlyric,artist,rawartist;
    public int vol;
    public Song(){}
    public Song(int id,String name,String rname,String lyric,String rlyric,int vol){
        this.ID = id;
        this.name = name;
        this.rawlyric = rlyric;
        this.rawname = rname;
        this.vol = vol;
    }
    
    public Song(String line){
        String[]lines = split(line,"\"");
        try{
            if(lines.length>10){
                ID = Integer.parseInt(lines[0].substring(0, lines[0].length()-1));
                name = lines[1];
                lyric = lines[3];
                rawname = lines[5];
                rawlyric = lines[7];
                vol = Integer.parseInt(lines[8].substring(1,lines[8].length()-1));
                artist = lines[9];
                rawartist = lines[11];
            }else{
                ID = Integer.parseInt(lines[0].substring(0, lines[0].length()-1));
                name = lines[1];
                lyric = lines[3];
                rawname = lines[5];
                rawlyric = lines[7];
                vol = Integer.parseInt(lines[8].substring(1,lines[8].length()-1));
                artist = "";
                rawartist = "";
            }
        }catch(Exception e){
            e.printStackTrace();
        }
    }
    
    public boolean isFav(){
        byte[]byteInputData = new byte[4];
        try {
            RecordStore rs = RecordStore.openRecordStore("karaoke", true);
            for (int x = 0; x <= rs.getNextRecordID(); x++){
                try{
                    byteInputData = rs.getRecord(x);
                    if(Int(byteInputData)==ID){
                        System.out.println("Favorite:ID="+ID+" at "+(x));
                        return true;
                    }
                }catch(Exception e){
                }
            }
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return false;
    }
    
    public void fav(){
        try {
            RecordStore rs = RecordStore.openRecordStore("karaoke", true);
            System.out.println("added:"+rs.addRecord(Bytes(ID), 0, 4));
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }
    
    public void unFav(){
        byte[]byteInputData = new byte[4];
        System.out.println("going to delete:"+ID);
        try {
            RecordStore rs = RecordStore.openRecordStore("karaoke", true);
            for (int x = 0; x <= rs.getNextRecordID(); x++){
                try{
                    byteInputData = rs.getRecord(x);
                    if(Int(byteInputData)==ID){
                        rs.deleteRecord(x);
                        System.out.println("delete:"+ID+" at "+x);
                    }
                }catch(Exception e){
                }
            }
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }
    
    public static final byte[] Bytes(int value) {
        return new byte[] {
                (byte)(value >>> 24),
                (byte)(value >>> 16),
                (byte)(value >>> 8),
                (byte)value};
    }
    public static final int Int(byte [] b) {
        return (b[0] << 24)
                + ((b[1] & 0xFF) << 16)
                + ((b[2] & 0xFF) << 8)
                + (b[3] & 0xFF);
    }
    public void gen(int v){
        rawartist = artist.toLowerCase();
        rawlyric = lyric.toLowerCase();
        vol = v;
        
    }
    
    String rmark(String s){
        s.charAt(0);
        return s;
    }
    /**
     * @param s : string to find
     * @return : 0 to 10
     * 
     */
    public int isNameABC(String s){
        if(s.equals("0")&&Character.isDigit(name.trim().charAt(0)))return 1;
        if(rawname.startsWith(s))return 1;
        if(name.toLowerCase().startsWith(s))return 1;
        return 1000;
    }
    public int isName(String s){
        if(rawname.indexOf(s)>=0)return 1;
        return 1000;
    }
    public int isArtist(String s){
        if(rawartist.equals(s))return 1;
        if(artist.toLowerCase().equals(s))return 1;
        return 1000;
    }
    public int isLyrics(String s){
        if(rawlyric.indexOf(s)>=0)return 1;
        if(lyric.toLowerCase().indexOf(s)>=0)return 1;
        return 1000;
    }
    
    public int isCode(int[]s){
        for(int i=0;i<s.length;i++)
            if(ID==s[i]&&vol<100)return 1;
        return 1000;
    }
    
    private String[] split(String original,String separator) {
        Vector nodes = new Vector();
        int index = original.indexOf(separator);
        while(index>=0) {
            nodes.addElement( original.substring(0, index) );
            original = original.substring(index+separator.length());
            index = original.indexOf(separator);
        }
        nodes.addElement( original );
        String[] result = new String[ nodes.size() ];
        if( nodes.size()>0 ) {
            for(int loop=0; loop<nodes.size(); loop++)
                result[loop] = (String)nodes.elementAt(loop);
        }
        return result;
    }
    public void out(){
        String s=""+ID+",\""+name+"\",";
        if(lyric!=null&&lyric.length()>0)s+="\""+lyric+"\",";
        else s+=",";
        s+="\""+rawname+"\",";
        if(rawlyric!=null&&rawlyric.length()>0)s+="\""+rawlyric+"\",";
        else s+=",";
        s+="45,";
        if(artist!=null&&artist.length()>0)s+="\""+artist+"\",";
        else s+=",";
        if(artist!=null&&artist.length()>0)s+="\""+rawartist+"\"";
        
        System.out.println(s);
    }
    
    static int[] getFavList(){
        byte[]byteInputData = new byte[4];
        int count=0;
        try {
            RecordStore rs = RecordStore.openRecordStore("karaoke", true);
            int[]list = new int[rs.getNumRecords()];
            for (int x = 0; x <= rs.getNextRecordID(); x++){
                try{
                    byteInputData = rs.getRecord(x);
                    list[count++] = Int(byteInputData);
                }catch(Exception e){
                }
            }
            return list;
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return new int[0];
    }
}
